import 'package:flutter/material.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/summary_entity.dart';
import 'package:library_base/ext/list_ext.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/button/ink_bg_well.dart';
import 'package:library_base/widget/dialog/dialog_util.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';
import 'package:library_base/base/base_list_state.dart';
import 'package:module_administrator/page/summary/summary_list_controller.dart';
import 'package:module_administrator/page/summary/summary_list_page_ui.dart';

class SummaryListPage extends StatefulWidget {
  const SummaryListPage({Key? key}) : super(key: key);

  @override
  _SummaryListPageState createState() => _SummaryListPageState();
}

class _SummaryListPageState
    extends BaseListState<SummaryEntity, SummaryEntity, SummaryListPage, SummaryListController> {
  @override
  SummaryListController registerController() => Get.find<SummaryListController>();

  @override
  void onCreate() {
    super.onCreate();
    setTitle(MultiLanguage.summaryOfRequirements.tr);
    setNavigationBarColor(ThemeUtil.getTheme().colorScheme.secondary);
  }

  @override
  void controllerOnCreate() {
    useRefresh = true;
    isCanRefresh = false;
    isCanLoad = false;
    enableControlFinishLoad = false;
    enableControlFinishRefresh = true;
    baseGetxController?.easyRefreshController = controller;
  }

  @override
  Widget getNavBarRightWidget() {
    return filterWell(
        baseGetxController!.stationTimeBegin.value, baseGetxController!.stationTimeEnd.value,
        onTap: (stationTimeBegin, stationTimeEnd) {
      var map = <String, dynamic>{};
      if (stationTimeBegin.isNotEmpty) {
        map['currentTime'] = DateTime.parse(stationTimeBegin);
      } else {
        DateTime dateTime= DateTime.now();
        int milliseconds = dateTime.millisecondsSinceEpoch+1000*60*60*24;
        map['currentTime'] = DateTime.fromMillisecondsSinceEpoch(milliseconds);
      }
      map['consignee'] = baseGetxController!.consignee;
      map['filter'] = baseGetxController!.filter;
      map['reset'] = baseGetxController!.reset;
      DialogUtil.showTimePickAndConsigneeDialog(arguments: map);
    });
  }

  @override
  Widget? getMainWidget(BuildContext context) {
    return MultiStateWidget(
      emptyWidget: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            LocalImage('admin_empty', package: Constant.packAdmin(), width: 0.6.sw),
            Text(MultiLanguage.noSuchProduct.tr, style: TextStyles.textSecondary28),
            SizedBox(height: 80.h),
            Row(mainAxisAlignment: MainAxisAlignment.center, children: [
              GradientButton(
                ThemeUtil.getTheme(),
                width: 0.4.sw,
                height: 80.w,
                text: MultiLanguage.reload.tr,
                onPressed: baseGetxController!.tryReLoad,
              ),
            ])
          ]),
      controller: baseGetxController!.multiControllerOut,
      child: Column(
        children: [
          Expanded(
            child: Container(
                color: ThemeUtil.getTheme().secondaryBgColor(),
                child: getListView(context, baseGetxController)),
          ),
          Container(
              height: 166.w,
              decoration: const BoxDecoration(color: Colors.white, boxShadow: [
                BoxShadow(
                    offset: Offset(0.0, 5.0), //阴影xy轴偏移量
                    blurRadius: 15.0, //阴影模糊程度
                    spreadRadius: -11 //阴影扩散程度
                    )
              ]),
              alignment: Alignment.topRight,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Expanded(
                      child: Stack(children: [
                    Positioned(
                      top: 43.w,
                      left: 24.w,
                      child: GetBuilder<SummaryListController>(
                        builder: (logic) {
                          return GestureDetector(
                            onTap: baseGetxController!.selectedAll,
                            child: Container(
                              color: Colors.transparent,
                              child: Row(
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  LocalImage(
                                      logic.isSelectAll.value
                                          ? 'admin_checked'
                                          : 'admin_no_checked',
                                      package: Constant.packAdmin(),
                                      width: 34.r,
                                      height: 34.r),
                                  gapW10,
                                  Text(MultiLanguage.selectedOrderCount
                                      .trArgs(["${logic.listDataSelected.length}"]))
                                ],
                              ),
                            ),
                          );
                        },
                      ),
                    ),
                  ])),
                  GetBuilder<SummaryListController>(
                    builder: (logic) {
                      return GradientButton(
                        ThemeUtil.getTheme(),
                        height: 77.w,
                        margin: EdgeInsets.only(right: 24.w, top: 21.w),
                        padding: EdgeInsets.symmetric(horizontal: 50.w),
                        text: MultiLanguage.generatePurchasingTasks.tr,
                        onPressed: baseGetxController!.listDataSelected.value.isEmpty
                            ? null
                            : baseGetxController!.chooseBuyerPage,
                      );
                    },
                  ),
                ],
              )),
        ],
      ),
    );
  }

  @override
  List<Widget> buildListItem(BuildContext context) {
    return baseGetxController!.listData.value.buildItem((index, element) {
      return GetBuilder<SummaryListController>(
        builder: (logic) {
          return GestureDetector(
              onTap: () {},
              child: buildSummaryItem(
                element,
                onTap: baseGetxController!.onItemClick,
                onExpand: baseGetxController!.onExpand,
                onTapChild: baseGetxController!.onTapChild,
              ));
        },
      );
    }, dividingLine: () {
      return gapH18;
    });
  }
}
